<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 1.4.&nbsp; Files and Directories</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/team.gif" width="60" height="17" border="0" align="absmiddle"  alt="Team BBL"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=ch01lev1sec3.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<a href=ch01lev1sec5.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="ch01lev1sec4"></a>
<h3 class="docSection1Title">1.4. Files and Directories</h3>
<a name="ch01lev2sec3"></a>
<H4 class="docSection2Title">File System</H4>
<p class="docText">The UNIX file system is a hierarchical arrangement of directories and files. Everything starts in the directory called <span class="docEmphasis">root</span> whose name is the single character <tt>/</tt>.</p>
<p class="docText">A <span class="docEmphasis">directory</span> is a file that contains directory entries. Logically, we can think of each directory entry as containing a filename along with a structure of information describing the attributes of the file. The attributes of a file are such things as type of fileregular file, directorythe size of the file, the owner of the file, permissions for the filewhether other users may access this fileand when the file was last modified. The <tt>stat</tt> and <tt>fstat</tt> functions return a structure of information containing all the attributes of a file. In <a class="docLink" href="ch04.html#ch04">Chapter 4</a>, we'll examine all the attributes of a file in great detail.</P>
<blockquote>
<p class="docText">We make a distinction between the logical view of a directory entry and the way it is actually stored on disk. Most implementations of UNIX file systems don't store attributes in the directory entries themselves, because of the difficulty of keeping them in synch when a file has multiple hard links. This will become clear when we discuss hard links in <a class="docLink" href="ch04.html#ch04">Chapter 4</a>.</P>
</blockquote>

<a name="ch01lev2sec4"></a>
<H4 class="docSection2Title">Filename</h4>
<p class="docText">The names in a directory are called <span class="docEmphasis">filenames</span>. The only two characters that cannot appear in a filename are the slash character (<tt>/</tt>) and the null character. The slash separates the filenames that form a pathname (described next) and the null character terminates a pathname. Nevertheless, it's good practice to restrict the characters in a filename to a subset of the normal printing characters. (We restrict the characters because if we use some of the shell's special characters in the filename, we have to use the shell's quoting mechanism to reference the filename, and this can get complicated.)</P>
<p class="docText">Two filenames are automatically created whenever a new directory is created: <tt>.</tt> (called <span class="docEmphasis">dot</span>) and <tt>..</tt> (called <span class="docEmphasis">dot-dot</span>). Dot refers to the current directory, and dot-dot refers to the parent directory. In the root directory, dot-dot is the same as dot.</P>
<p class="docText">The Research UNIX System and some older UNIX System V file systems restricted a filename to 14 characters. BSD versions extended this limit to 255 characters. Today, almost all commercial UNIX file systems support at least 255-character filenames.</P>

<a name="ch01lev2sec5"></a>
<h4 class="docSection2Title">Pathname</H4>
<p class="docText"><a name="idd1e2170"></a><a name="idd1e2173"></a><a name="idd1e2176"></a><a name="idd1e2181"></a><a name="idd1e2186"></a><a name="idd1e2191"></a><a name="idd1e2196"></a><a name="idd1e2201"></a><a name="idd1e2204"></a><a name="idd1e2209"></a><a name="idd1e2214"></a><a name="idd1e2219"></a>A sequence of one or more filenames, separated by slashes and optionally starting with a slash, forms a <span class="docEmphasis">pathname</span>. A pathname that begins with a slash is called an <span class="docEmphasis">absolute pathname</span>; otherwise, it's called a <span class="docEmphasis">relative pathname</span>. Relative pathnames refer to files relative to the current directory. The name for the root of the file system (<tt>/</tt>) is a special-case absolute pathname that has no filename component.</p>
<a name="ch01ex01"></a>
<H5 class="docExampleTitle">Example</H5>
<p class="docText">Listing the names of all the files in a directory is not difficult. <a class="docLink" href="#ch01fig03">Figure 1.3</a> shows a bare-bones implementation of the <tt>ls</tt>(1) command.</P>
<p class="docText">The notation <tt>ls</tt>(1) is the normal way to reference a particular entry in the UNIX system manuals. It refers to the entry for <tt>ls</tt> in Section 1. The sections are normally numbered 1 through 8, and all the entries within each section are arranged alphabetically. Throughout this text, we assume that you have a copy of the manuals for your UNIX system.</p>
<blockquote>
<p class="docText">Historically, UNIX systems lumped all eight sections together into what was called the <span class="docEmphasis">UNIX Programmer's Manual</span>. As the page count increased, the trend changed to distributing the sections among separate manuals: one for users, one for programmers, and one for system administrators, for example.</P>
<p class="docText">Some UNIX systems further divide the manual pages within a given section, using an uppercase letter. For example, all the standard input/output (I/O) functions in AT&amp;T [<a class="docLink" href="bib01.html#biblio01_011">1990e</a>] are indicated as being in Section 3S, as in <tt>fopen</tt>(3S). Other systems have replaced the numeric sections with alphabetic ones, such as C for commands.</P>
</blockquote>
<p class="docText"><a name="idd1e2279"></a><a name="idd1e2284"></a><a name="idd1e2289"></a><a name="idd1e2294"></a>Today, most manuals are distributed in electronic form. If your manuals are online, the way to see the manual pages for the <tt>ls</tt> command would be something like</p>

<pre>
   man 1 ls
</pre><BR>

<p class="docText">or</P>

<pre>
   man -s1 ls
</pre><br>

<p class="docText"><a class="docLink" href="#ch01fig03">Figure 1.3</a> is a program that just prints the name of every file in a directory, and nothing else. If the source file is named <tt>myls.c</tt>, we compile it into the default <tt>a.out</tt> executable file by</p>

<pre>
   cc myls.c
</pre><br>

<blockquote>
<p class="docText">Historically, <tt>cc(1)</tt> is the C compiler. On systems with the GNU C compilation system, the C compiler is <tt>gcc</tt>(1). Here, <tt>cc</tt> is often linked to <tt>gcc</tt>.</p>
</blockquote>
<p class="docText">Some sample output is</P>

<pre>
   $ <span class="docEmphStrong">./a.out /dev</span>
   .
   ..
   console
   tty
   mem
   kmem
   null
   mouse
   stdin
   stdout
   stderr
   zero
                       <span class="docEmphItalicAlt">many more lines that aren't shown</span>
   cdrom
   $ <span class="docEmphStrong">./a.out /var/spool/cron</span>
   can't open /var/spool/cron: Permission denied
   $ <span class="docEmphStrong">./a.out /dev/tty</span>
   can't open /dev/tty: Not a directory
</pre><br>

<p class="docText">Throughout this text, we'll show commands that we run and the resulting output in this fashion: Characters that we type are shown in <span class="docEmphStrong"><tt>this font</tt></span>, whereas output from programs is shown <tt>like this</tt>. If we need to add comments to this output, we'll show the comments in <span class="docEmphasis">italics</span>. The dollar sign that precedes our input is the prompt that is printed by the shell. We'll always show the shell prompt as a dollar sign.</P>
<p class="docText">Note that the directory listing is not in alphabetical order. The <tt>ls</tt> command sorts the names before printing them.</p>
<p class="docText">There are many details to consider in this 20-line program.</P>
<ul><li><p class="docList">First, we include a header of our own: <tt>apue.h</tt>. We include this header in almost every program in this text. This header includes some standard system headers and defines numerous constants and function prototypes that we use throughout the examples in the text. A listing of this header is in <a class="docLink" href="app02.html#app02">Appendix B</a>.</p></li><li><p class="docList"><a name="idd1e2403"></a><a name="idd1e2406"></a><a name="idd1e2411"></a><a name="idd1e2416"></a><a name="idd1e2421"></a><a name="idd1e2426"></a><a name="idd1e2431"></a><a name="idd1e2436"></a><a name="idd1e2441"></a><a name="idd1e2446"></a><a name="idd1e2451"></a><a name="idd1e2456"></a><a name="idd1e2459"></a><a name="idd1e2464"></a><a name="idd1e2469"></a><a name="idd1e2474"></a><a name="idd1e2479"></a><a name="idd1e2484"></a><a name="idd1e2487"></a><a name="idd1e2492"></a><a name="idd1e2497"></a>The declaration of the <tt>main</tt> function uses the style supported by the ISO C standard. (We'll have more to say about the ISO C standard in the next chapter.)</p></li><li><p class="docList">We take an argument from the command line, <tt>argv[1]</tt>, as the name of the directory to list. In <a class="docLink" href="ch07.html#ch07">Chapter 7</a>, we'll look at how the <tt>main</tt> function is called and how the command-line arguments and environment variables are accessible to the program.</p></li><li><p class="docList">Because the actual format of directory entries varies from one UNIX system to another, we use the functions <tt>opendir</tt>, <tt>readdir</tt>, and <tt>closedir</tt> to manipulate the directory.</p></li><li><p class="docList">The <tt>opendir</tt> function returns a pointer to a <tt>DIR</tt> structure, and we pass this pointer to the <tt>readdir</tt> function. We don't care what's in the <tt>DIR</tt> structure. We then call <tt>readdir</tt> in a loop, to read each directory entry. The <tt>readdir</tt> function returns a pointer to a <tt>dirent</tt> structure or, when it's finished with the directory, a null pointer. All we examine in the <tt>dirent</tt> structure is the name of each directory entry (<tt>d_name</tt>). Using this name, we could then call the <tt>stat</tt> function (<a class="docLink" href="ch04lev1sec2.html#ch04lev1sec2">Section 4.2</a>) to determine all the attributes of the file.</p></li><li><p class="docList">We call two functions of our own to handle the errors: <tt>err_sys</tt> and <tt>err_quit</tt>. We can see from the preceding output that the <tt>err_sys</tt> function prints an informative message describing what type of error was encountered (&quot;Permission denied&quot; or &quot;Not a directory&quot;). These two error functions are shown and described in <a class="docLink" href="app02.html#app02">Appendix B</a>. We also talk more about error handling in <a class="docLink" href="ch01lev1sec7.html#ch01lev1sec7">Section 1.7</a>.</P></LI><li><p class="docList">When the program is done, it calls the function <tt>exit</tt> with an argument of 0. The function <tt>exit</tt> terminates a program. By convention, an argument of 0 means OK, and an argument between 1 and 255 means that an error occurred. In <a class="docLink" href="ch08lev1sec5.html#ch08lev1sec5">Section 8.5</a>, we show how any program, such as a shell or a program that we write, can obtain the <tt>exit</tt> status of a program that it executes.</P></LI></UL>

<a name="ch01fig03"></a>
<h5 class="docExampleTitle">Figure 1.3. List all the files in a directory</H5>

<pre>
#include "apue.h"
#include &lt;dirent.h&gt;

int
main(int argc, char *argv[])
{
    DIR             *dp;
    struct dirent   *dirp;

    if (argc != 2)
        err_quit("usage: ls directory_name");

    if ((dp = opendir(argv[1])) == NULL)
        err_sys("can't open %s", argv[1]);
    while ((dirp = readdir(dp)) != NULL)
        printf("%s\n", dirp-&gt;d_name);

    closedir(dp);
    exit(0);
}
</pre><BR>



<a name="ch01lev2sec6"></a>
<H4 class="docSection2Title">Working Directory</h4>
<p class="docText">Every process has a <span class="docEmphasis">working directory</span>, sometimes called the <span class="docEmphasis">current working directory</span>. This is the directory from which all relative pathnames are interpreted. A process can change its working directory with the <tt>chdir</tt> function.</P>
<p class="docText">For example, the relative pathname <tt>doc/memo/joe</tt> refers to the file or directory <tt>joe</tt>, in the directory <tt>memo</tt>, in the directory <tt>doc</tt>, which must be a directory within the working directory. From looking just at this pathname, we know that both <tt>doc</tt> and <tt>memo</tt> have to be directories, but we can't tell whether <tt>joe</tt> is a file or a directory. The pathname <tt>/usr/lib/lint</tt> is an absolute pathname that refers to the file or directory <tt>lint</tt> in the directory <tt>lib</tt>, in the directory <tt>usr</tt>, which is in the root directory.</p>

<a name="ch01lev2sec7"></a>
<H4 class="docSection2Title">Home Directory</H4>
<p class="docText">When we log in, the working directory is set to our <span class="docEmphasis">home directory</span>. Our home directory is obtained from our entry in the password file (<a class="docLink" href="ch01lev1sec3.html#ch01lev1sec3">Section 1.3</a>).</P>


<a href="17021535.html"><img src="images/pixel.gif" alt="" width="1" height="1" border="0"></a><ul></UL></TD></tr></table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/team.gif" width="60" height="17" border="0" align="absmiddle"  alt="Team BBL"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=ch01lev1sec3.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<a href=ch01lev1sec5.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
</body></html><br>
<table width="100%" cellspacing="0" cellpadding="0"
style="margin-top: 0pt; border-collapse: collapse;"> 
<tr> <td align="right" style="background-color=white; border-top: 1px solid gray;"> 
<a href="http://www.zipghost.com/" target="_blank" style="font-family: Tahoma, Verdana;
 font-size: 11px; text-decoration: none;">The CHM file was converted to HTM by Trial version of <b>ChmD<!--204-->ecompiler</b>.</a>
</TD>
</TR><tr>
<td align="right" style="background-color=white; "> 
<a href="http://www.etextwizard.com/download/cd/cdsetup.exe" target="_blank" style="font-family: Tahoma, Verdana;
 font-size: 11px; text-decoration: none;">Download <b>ChmDec<!--204-->ompiler</b> at: http://www.zipghost.com</a>
</TD></tr></table>
